package hlhj.fhp.newslib.fragments

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import ezy.ui.view.BannerView
import hlhj.fhp.newslib.BaseAty
import hlhj.fhp.newslib.BaseFgm
import hlhj.fhp.newslib.Constant
import hlhj.fhp.newslib.R
import hlhj.fhp.newslib.activitys.ImageInfoAty
import hlhj.fhp.newslib.activitys.SpecialInfoAty
import hlhj.fhp.newslib.adapters.CatMultiAdp
import hlhj.fhp.newslib.javabean.AdVBean
import hlhj.fhp.newslib.javabean.CatBean
import hlhj.fhp.newslib.javabean.ColumBean
import hlhj.fhp.newslib.network.Apis
import hlhj.fhp.newslib.network.CustomAction
import hlhj.fhp.newslib.network.LoadingAction
import hlhj.fhp.newslib.network.Urls
import hlhj.fhp.newslib.utils.GlideUtil
import hlhj.fhp.newslib.utils.ToastUtil
import kotlinx.android.synthetic.main.hlhj_news_head_lo.*
import kotlinx.android.synthetic.main.recommend_video.*
import kotlinx.android.synthetic.main.recomment_fgm.*
import kotlinx.android.synthetic.main.recomment_fgm.view.*
import tmproject.hlhj.fhp.adplayerhelper.ADVBean
import java.util.ArrayList

/**
 * Created by Administrator on 2018\6\5 0005.
 */
class RecommendFgm :BaseFgm(), SpringView.OnFreshListener {
    private var page=1;
    private var num=0
    private lateinit var adp:BaseMultiItemQuickAdapter<CatBean.DataBean.ListBeanX,BaseViewHolder>
    private val mutiDatas=ArrayList<CatBean.DataBean.ListBeanX>()

    companion object {
        fun getInstence(id:Int,name:String): RecommendFgm {
            val recommendFgm = RecommendFgm()
            val bundle = Bundle()
            bundle.putInt("id",id)
            bundle.putString("name",name)
            recommendFgm.arguments=bundle
            return recommendFgm
        }

    }
    override fun getContentId(): Int {
        return  R.layout.recomment_fgm
    }

    override fun initListener() {
    }
    fun dispatchData(){
        Apis.getCat(arguments!!.getInt("id",0),page,num)
                .doOnSubscribe{
                    loadingDialog.show()
                }
                .subscribe(object :CustomAction<Response<CatBean>>{
                    override fun onResult(t: Response<CatBean>) {
                        rootView.spView.onFinishFreshAndLoad()
                        loadingDialog.dismiss()
                        val catBean = t.body()
                        Log.e("fhppa","执行一次${t.isFromCache}")
                        if (catBean.data!=null){
                            num=catBean.data.number
                            if (!t.isFromCache){
                                if (page==1) mutiDatas.clear()
                            }
                            mutiDatas.addAll(catBean.data.list)
                            Log.e("fhppa","加一次${this@RecommendFgm}")
                            adp.notifyDataSetChanged()
                        }

                    }
                })
    }
    fun dispatchNewData(){
        Apis.getCat(arguments!!.getInt("id",0),1,0)
                .doOnSubscribe{
                    loadingDialog.show()
                }
                .subscribe(object :CustomAction<Response<CatBean>>{
                    override fun onResult(t: Response<CatBean>) {
                        rootView.spView.onFinishFreshAndLoad()
                        loadingDialog.dismiss()
                        val catBean = t.body()
                        Log.e("fhppa","执行一次${t.isFromCache}")
                        if (catBean.data!=null){
                            num=catBean.data.number
                            if (!t.isFromCache){
                                if (page==1) mutiDatas.clear()
                            }
                            mutiDatas.clear()
                            page=1
                            num=0
                            mutiDatas.addAll(catBean.data.list)
                            Log.e("fhppa","加一次${this@RecommendFgm}")
                            adp.notifyDataSetChanged()
                        }

                    }
                })
    }
    override fun initView() {
        /*上下拉*/
        rootView.spView.header=DefaultHeader(context)
        rootView.spView.footer=DefaultFooter(context)
        rootView.spView.setListener(this)
        /*适配器*/
        adp=CatMultiAdp(mutiDatas,activity!!)
        rootView.recyclerview.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        rootView.recyclerview.adapter=adp
        /*获取数据*/
        dispatchData()

       Apis.getADVs()?.let {
           it.subscribe(object :CustomAction<Response<ADVBean>>{
               override fun onResult(t: Response<ADVBean>) {
                   val body = t.body()
                   Constant.ads.addAll(body.data)
               }
           })
       }


    }

    override fun onLoadmore() {
        page++

        dispatchData()
    }

    override fun onRefresh() {
        dispatchNewData()
    }
}